package com.xinzhi.log.service.impl;

import com.xinzhi.log.service.ITransferLog;
import com.xinzhi.mapper.IncomeAndExpensesMapper;
import com.xinzhi.mapper.TransferRecordMapper;
import com.xinzhi.pojo.Bankaccount;
import com.xinzhi.pojo.Banktransaction;
import com.xinzhi.pojo.Incomeandexpenditurerecord;
import com.xinzhi.service.ITransferRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

@Service
public class TransferLogImpl implements ITransferLog {

    @Autowired
    private TransferRecordMapper transferRecordMapper;
    @Override
    @Async("taskExecutor")
    public void addTransfer(Bankaccount from, Bankaccount to, Double money) {
        Banktransaction transfer = new Banktransaction();
        Banktransaction banktransaction = new Banktransaction();
        banktransaction.setInitiatorName(from.getUsername());
        banktransaction.setInitiatorPhone(from.getPhoneNumber());
        banktransaction.setInitiatorCardNumber(from.getCardNumber());
        banktransaction.setRecipientName(to.getUsername());
        banktransaction.setRecipientPhone(to.getPhoneNumber());
        banktransaction.setRecipientCardNumber(to.getCardNumber());

        transfer.setTransferAmount(money);
        transfer.setRecipientBalance(to.getMoney() + money);
        transfer.setInitiatorBalance(from.getMoney() - money);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA);
        // 获取当前时间并格式化
        LocalDateTime now = LocalDateTime.now();
        transfer.setTransactionTime(now.format(formatter));
        transferRecordMapper.insertAll(banktransaction);
    }
}
